import 'package:boss/color_constant.dart';
import 'package:boss/main.dart';
import 'package:boss/textStyle/text_style.dart';
import 'package:boss/ui/page/storeEvaluate/provider/provider.dart';
import 'package:boss/ui/widget/loading/loading.dart';
import 'package:hi_common/hi_common.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

class BottomKeyboardInput extends StatefulWidget {
  @override
  BottomKeyboardInputState createState() => BottomKeyboardInputState();
}

class BottomKeyboardInputState extends State<BottomKeyboardInput> with WidgetsBindingObserver {
  TextEditingController textEditing = TextEditingController();

  FocusNode focusNode = FocusNode();

  var provider;

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addObserver(this);
    provider = Provider.of<StoreCommentProvider>(
      context,
      listen: false,
    );
    focusNode.addListener(() {
      if (!focusNode.hasFocus) {
        provider.replyClickIndex = null;
        provider.notifyListeners();
      }
    });
  }

  @override
  void didChangeMetrics() {
    super.didChangeMetrics();
    WidgetsBinding.instance.addPostFrameCallback((_) {
      setState(() {
        Future.delayed(Duration(milliseconds: 0), () async {
          if (MediaQuery.of(context).viewInsets.bottom == 0) {
            //关闭键盘
            provider.replyClickIndex = null;
            provider.notifyListeners();
            focusNode.unfocus();
          } else {
            //显示键盘
          }
          provider.keyboardHeight = num.parse(MediaQuery.of(context).viewInsets.bottom.toString());
          provider.notifyListeners();
        });
      });
    });
  }

  @override
  void dispose() {
    // TODO: implement dispose
    textEditing.dispose();
    WidgetsBinding.instance.removeObserver(this);

    super.dispose();
  }

  //回复内容
  replyContent(data, reply, index) async {
    try {
      showLoadingToast();
      var res = await api.replyAdd({
        'commentId': data["id"],
        'reply': reply,
      });
      if (res.successful()) {
        toast("回复成功!");

        provider.commentListArrAdd(reply, index);
        provider.replyClickIndex = null;
        provider.notifyListeners();
        focusNode.unfocus();
      } else {
        toast(res.msg);
      }
    } catch (e) {} finally {
      hideLoadingToast();
    }
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.symmetric(
        vertical: 16.w,
      ),
      height: 80.w,
      color: Colors.white,
      width: ScreenUtil.screenWidthDp,
      child: Row(
        children: <Widget>[
          Expanded(
            child: Container(
              padding: EdgeInsets.only(
                left: 12.w,
                right: 8.w,
              ),
              child: TextFormField(
                maxLength: 280,
                focusNode: focusNode,
                autofocus: true,
                controller: textEditing,
                keyboardAppearance: Brightness.light,
//                focusNode: focusNode,
//                controller: controller,
//                onChanged: onChanged,
//                onEditingComplete: onEditingComplete,
                decoration: InputDecoration(
                  counterText: '',
                  hintText: "请输入回复内容",
                  hintStyle: TextStyleConstant().hintTextStyle,
                  contentPadding: EdgeInsets.symmetric(
//                    vertical: ScreenUtil.instance.setHeight(),
                    horizontal: 26.w,
                  ),
                  border: OutlineInputBorder(
                    borderSide: BorderSide(
                      color: ColorConstant.e5e5,
                      width: 1.px,
                    ),
                    borderRadius: BorderRadius.circular(
                      60.w,
                    ),
                  ),
                  enabledBorder: OutlineInputBorder(
                    borderSide: BorderSide(
                      color: ColorConstant.e5e5,
                      width: 1.px,
                    ),
                    borderRadius: BorderRadius.circular(
                      60.w,
                    ),
                  ),
                  focusedBorder: OutlineInputBorder(
                    borderSide: BorderSide(
                      color: ColorConstant.e5e5,
                      width: 1.px * 2,
                    ),
                    borderRadius: BorderRadius.circular(
                      60.w,
                    ),
                  ),
                ),
                style: TextStyleConstant().normal_1_28,
//                enabled:false,
              ),
            ),
          ),
          Padding(
            padding: EdgeInsets.only(
              left: 2.w,
              right: 10.w,
            ),
            child: Container(
              width: 120.w,
              child: RaisedButton(
                elevation: 0,
                //按钮下面的阴影
                highlightElevation: 0,
                child: Text(
                  "确定",
                  style: TextStyleConstant().white_26,
                ),
                color: ColorConstant.ff5770f6,
                textColor: Colors.white,
                onPressed: () {
                  if (textEditing.text.trim() == '') {
                    toast("回复内容不能为空!");
                    return;
                  }
                  var provider = Provider.of<StoreCommentProvider>(
                    context,
                    listen: false,
                  );
                  replyContent(provider.commentListArr[provider.replyClickIndex], textEditing.text, provider.replyClickIndex);
                },
                disabledColor: Colors.grey,
                disabledTextColor: Colors.white,
                disabledElevation: 4,
                shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12.0)), //圆角大小
              ),
            ),
          ),
        ],
      ),
    );
  }
}
